package com.ymatou.iapi.godmch.testcase;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.CheckPermissionBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.service.CheckPermissionCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
/**
*校验客服权限
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CheckPermission{
	private static CheckPermissionBean checkpermissionBean;
	private static CheckPermissionCall checkpermissionCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("校验客服权限");
	}

	@Before
	public void caseUp() {
		checkpermissionBean=new CheckPermissionBean();
		checkpermissionCall=new CheckPermissionCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(checkpermissionCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckPermission_001() {
		Logger.start(true,"校验客服权限,成功-销售专员");
		try {
			Integer templateId=1;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
	        int[] permissionCodes = new DefGodMchWapper().selectPermission(templateId).stream().mapToInt(mapper->mapper.getPermissionCode()).toArray();
	        //获取templateid权限列表 因为csid是templateId=1 相对固定
			List<Integer> listpermissionCodes = Arrays.stream(permissionCodes).boxed().collect(Collectors.toList());
			
	        GodmchManageCallService.ALLPermissionList.forEach(p->{
	        	checkpermissionBean.setPermissionCode(p);
				checkpermissionBean.setCsId(csId);
				checkpermissionCall.setData(checkpermissionBean);
				try {
					checkpermissionCall.callService();
					Logger.verifyEquals("200", checkpermissionCall.getString("status"), "验证status");
					boolean hasp=checkpermissionCall.getJsonObject().get("result").getAsJsonObject().get("has").getAsBoolean();
					Logger.verifyEquals(listpermissionCodes.contains(p), hasp, "验证是否有权限");
				} catch (IOException e) {
					e.printStackTrace();
				}			
	        });
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckPermission_002() {
		Logger.start(true,"校验客服权限,成功-发货专员");
		try {
			Integer templateId=2;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
	        int[] permissionCodes = new DefGodMchWapper().selectPermission(templateId).stream().mapToInt(mapper->mapper.getPermissionCode()).toArray();
	        //获取templateid权限列表 因为csid是templateId=1 相对固定
			List<Integer> listpermissionCodes = Arrays.stream(permissionCodes).boxed().collect(Collectors.toList());
			
	        GodmchManageCallService.ALLPermissionList.forEach(p->{
	        	checkpermissionBean.setPermissionCode(p);
				checkpermissionBean.setCsId(csId);
				checkpermissionCall.setData(checkpermissionBean);
				try {
					checkpermissionCall.callService();
					Logger.verifyEquals("200", checkpermissionCall.getString("status"), "验证status");
					boolean hasp=checkpermissionCall.getJsonObject().get("result").getAsJsonObject().get("has").getAsBoolean();
					Logger.verifyEquals(listpermissionCodes.contains(p), hasp, "验证是否有权限");
				} catch (IOException e) {
					e.printStackTrace();
				}			
	        });
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckPermission_003() {
		Logger.start(true,"校验客服权限,成功-普通运营");
		try {
			Integer templateId=3;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
	        int[] permissionCodes = new DefGodMchWapper().selectPermission(templateId).stream().mapToInt(mapper->mapper.getPermissionCode()).toArray();
	        //获取templateid权限列表 因为csid是templateId=1 相对固定
			List<Integer> listpermissionCodes = Arrays.stream(permissionCodes).boxed().collect(Collectors.toList());
			
	        GodmchManageCallService.ALLPermissionList.forEach(p->{
	        	checkpermissionBean.setPermissionCode(p);
				checkpermissionBean.setCsId(csId);
				checkpermissionCall.setData(checkpermissionBean);
				try {
					checkpermissionCall.callService();
					Logger.verifyEquals("200", checkpermissionCall.getString("status"), "验证status");
					boolean hasp=checkpermissionCall.getJsonObject().get("result").getAsJsonObject().get("has").getAsBoolean();
					Logger.verifyEquals(listpermissionCodes.contains(p), hasp, "验证是否有权限");
				} catch (IOException e) {
					e.printStackTrace();
				}			
	        });	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckPermission_004() {
		Logger.start(true,"校验客服权限,成功-高级运营");
		try {
			Integer templateId=4;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
	        int[] permissionCodes = new DefGodMchWapper().selectPermission(templateId).stream().mapToInt(mapper->mapper.getPermissionCode()).toArray();
	        //获取templateid权限列表 因为csid是templateId=1 相对固定
			List<Integer> listpermissionCodes = Arrays.stream(permissionCodes).boxed().collect(Collectors.toList());
			
	        GodmchManageCallService.ALLPermissionList.forEach(p->{
	        	checkpermissionBean.setPermissionCode(p);
				checkpermissionBean.setCsId(csId);
				checkpermissionCall.setData(checkpermissionBean);
				try {
					checkpermissionCall.callService();
					Logger.verifyEquals("200", checkpermissionCall.getString("status"), "验证status");
					boolean hasp=checkpermissionCall.getJsonObject().get("result").getAsJsonObject().get("has").getAsBoolean();
					Logger.verifyEquals(listpermissionCodes.contains(p), hasp, "验证是否有权限");
				} catch (IOException e) {
					e.printStackTrace();
				}			
	        });
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckPermission_005() {
		Logger.start(true,"校验客服权限,不输入csid失败");
		try {
			checkpermissionBean.setPermissionCode(101);
//			checkpermissionBean.setCsId(csId);
			checkpermissionCall.setData(checkpermissionBean);
			checkpermissionCall.callService();			
			Logger.verifyEquals("4001", checkpermissionCall.getString("status"), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckPermission_006() {
		Logger.start(true,"校验客服权限,csid已移除没有权限");
		try {
			Integer templateId=4;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
	        GodmchManageCallService.RemoveCs(mchId, csId);
	        Logger.comment("移除客服:"+csId);
			checkpermissionBean.setPermissionCode(101);
			checkpermissionBean.setCsId(csId);
			checkpermissionCall.setData(checkpermissionBean);
			checkpermissionCall.callService();
			Logger.verifyEquals("5000", checkpermissionCall.getString("status"), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckPermission_007() {
		Logger.start(true,"校验客服权限,不输入permissionCode失败");
		try {
			checkpermissionBean.setCsId(1);
			checkpermissionCall.setData(checkpermissionBean);
			checkpermissionCall.callService();			
			Logger.verifyEquals("4001", checkpermissionCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}